﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Lemonc.Tools
{
  public static  class LogCls
    {
      private static XmlDocument xmlDoc;
      public static void WriteLog(string info)
      {
          string today = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
          string logName = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + ".txt";
          string conf = AppDomain.CurrentDomain.BaseDirectory + "\\Config";
          string logPath = conf + "/" + today;
          if (!Directory.Exists(logPath))
              Directory.CreateDirectory(logPath);
          string log = Path.Combine(logPath, logName);

          using (Stream fs = new FileStream(log, FileMode.OpenOrCreate, FileAccess.Write))
          {
              fs.Seek(0, SeekOrigin.End);

              using (TextWriter m_streamWriter = new StreamWriter(fs))
              {


                  m_streamWriter.WriteLine(info + "-----" + DateTime.Now.ToString() + "\n");

                  m_streamWriter.Flush();


                  m_streamWriter.Close();

                  fs.Close();
              }
          }
      }

      public static void WriteLog1(string info)
      {

          string today = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
          string logName = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + ".txt";
          string pathSplit = System.IO.Path.DirectorySeparatorChar.ToString();

          string conf = "";
          String sysPath = System.Environment.SystemDirectory.ToString();
          sysPath = sysPath.Substring(0, sysPath.IndexOf(pathSplit) + pathSplit.Length);
          conf = System.IO.Path.Combine(sysPath, "dataexchange");
          if (!System.IO.Directory.Exists(conf))
          {
              System.IO.Directory.CreateDirectory(conf);
          }
          string sysConfig = Path.Combine(conf, "config.xml");
          if (System.IO.File.Exists(sysConfig))
          {
              xmlDoc = new XmlDocument();
              xmlDoc.Load(sysConfig);
              XmlNode xnNode = xmlDoc.SelectSingleNode("root/configpath");
              conf = Convert.ToString(xnNode.Attributes["value"].Value);
          }
          else
          {
              conf = "";
          }

          string logPath = conf + "/" + today;
          if (!Directory.Exists(logPath))
              Directory.CreateDirectory(logPath);
          string log = Path.Combine(logPath, logName);

          using (Stream fs = new FileStream(log, FileMode.OpenOrCreate, FileAccess.Write))
          {
              fs.Seek(0, SeekOrigin.End);

              using (TextWriter m_streamWriter = new StreamWriter(fs))
              {


                  m_streamWriter.WriteLine(info + "-----" + DateTime.Now.ToString() + "\n");

                  m_streamWriter.Flush();


                  m_streamWriter.Close();

                  fs.Close();
              }
          }
      }
    }
}
